package owen.utils;

import java.io.*;

public class FileSearch {
	public void getFilePath(StringBuffer filePath, String dir, String start,
			String end) {
		try {
			String sep = File.separator;//兼容Linux与Windows 
			File path = new File(dir);
			String[] list = path.list(new DirFilter(""));
			for (int i = 0; i < list.length; i++) {
				File f = new File(dir + sep + list[i]);
				if (!f.isFile()) {
					getFilePath(filePath, dir + sep + list[i], start, end);
				} else {
					if (list[i].startsWith(start)) {
						if (list[i].endsWith(end)) {
							filePath.append(dir + sep + list[i]);
							filePath.append(";");
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//得到指定目录下的文件名
	public void getFileName(StringBuffer filePath, String dir, String start,
			String end) {
		try {
			String sep = File.separator;//兼容Linux与Windows modify by moxb@20160328
			File path = new File(dir);
			String[] list = path.list(new DirFilter(""));
			for (int i = 0; i < list.length; i++) {
				File f = new File(dir + sep + list[i]);
				if (!f.isFile()) {
					getFilePath(filePath, dir + sep + list[i], start, end);
				} else {
					if (list[i].startsWith(start)) {
						if (list[i].endsWith(end)) {
							filePath.append(list[i]+";");							
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		FileSearch obj = new FileSearch();
		StringBuffer sb = new StringBuffer();
		obj.getFilePath(sb, "E:\\workspace\\testpas\\WebRoot",
				"struts-config-", "xml");
		String[] path = sb.toString().split(";");
		for (int i = 0; i < path.length; i++) {
			System.out.println(path[i]);
		}
	}

}

class DirFilter implements FilenameFilter {
	String fileName;

	DirFilter(String fileName) {
		this.fileName = fileName;
	}

	public boolean accept(File dir, String name) {
		String f = new File(name).getName();
		return f.indexOf(fileName) != -1;
	}
}